<?php

class Catgame extends AppModel {

    var $alias = 'Catgame';
    var $name = 'Catgame';
    var $useTable = 'game_categories';
    var $hasMany = array(
        'Game' => array(
            'className' => 'Game',
            'foreignKey' => 'cat_id',
            'dependent' => true,
        )
    );

    function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
        $parameters = compact('conditions');
        $this->recursive = $recursive;
        $count = $this->find('count', array_merge($parameters, $extra));
        if (isset($extra['group'])) {
            $count = $this->getAffectedRows();
        }
        return $count;
    }
    function setListCategoryCache(){
        $cacheName  =   'list-cat-info';
        $data = $this->find('all', array(
                    'conditions' => array('Catgame.published' => '1'),
                    'fields' => 'Catgame.id, Catgame.title, Catgame.alias, Catgame.total_games',
                    'recursive'=>   -1
        ));
        $cat_list   =   array();
        foreach($data AS $row){
            $cat_list[$row['Catgame']['id']]    =   $row['Catgame'];
        }
        Cache::write($cacheName, $cat_list, 'games');
    }
    function getListAll() {
        
        $data = $this->find('list', array(
                    'fields' => 'Catgame.id, Catgame.title',
                ));
        
        return $data;
    }
    function writeCategoryGame($limit = 15){
        $data = $this->find('all', array(
                    'conditions' => array('Catgame.published' => '1'),
                    'fields' => array('Catgame.id', 'Catgame.alias', 'Catgame.title'),
                    'order' => 'Catgame.ordering ASC',
                    'limit' => $limit,
                    'offset'    =>  0
                ));
        $html   =   '';

            for($i = 0; $i < $limit; $i++){
                $row    =   $data[$i];
                $link_url   =   "/danh-muc-game/{$row['Catgame']['id']}-{$row['Catgame']['alias']}.html";
                $html .= '<div class="row-cat-game">';
                $html .= '<p><a href="' . $link_url . '" title="' . $row['Catgame']['title'] . '">' . $row['Catgame']['title'] . '</a></p>';
                $html .= '</div>';
            }
            $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'categories_game.ctp', 'w');
            fwrite($resourse, $html);
        
    }
}

?>
